package com.hejia.alauda.service;

import java.util.Date;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.hejia.alauda.entity.Question;
import com.hejia.alauda.entity.example.QuestionExample;
import com.hejia.alauda.persistence.mybatis.QuestionMapper;
import com.hejia.alauda.utils.Pager;
//import com.umpay.api.util.StringUtil;

@Service
public class QuestionServiceImpl implements QuestionService {

	@Resource
	private QuestionMapper questionMapper;

	@Override
	public Pager<Question> findQuestions(Pager<Question> pager, String ask) {

		QuestionExample example = new QuestionExample();
		if (StringUtils.isNotEmpty(ask)) {
			example.appendCriterion("ask like", "%" + ask + "%");
		}
		example.setOrderByClause("id desc");
		pager.setList(this.questionMapper.selectByExample(example, pager));
		pager.setTotalCount(this.questionMapper.countByExample(example));

		return pager;
	}

	@Override
	public Question addQuestion(Question question) {
		question.setCreateDate(new Date());
		question.setModifyDate(new Date());
		this.questionMapper.insert(question);
		return question;
	}

	@Override
	public void deleteQuestion(Long id) {
		this.questionMapper.deleteByPrimaryKey(questionMapper
				.selectByPrimaryKey(id));
	}

	@Override
	public boolean editQuestion(Question question) {
		return this.questionMapper.updateByPrimaryKey(question) > 0;
	}

	@Override
	public Question findQuestionById(Long id) {
		return this.questionMapper.selectByPrimaryKey(id);
	}

}
